﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.logMoneyChange
{
    public partial class Edit : EditPagebase<IMoneyChangeContract, MoneyChange>
    {
        public IUserContract UserContract { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //FineUIListHelper.BindDDL<User>(UserContract.GetResolvedTree(), 0L, ed_User);
                //FineUIListHelper.BindDDL(UserContract.EntityName, this.UserContract.GetSelectList(), 0L, ed_User);
                if (PageMode != pagemode.Add)
                    LoadData();
            }
        }

        protected override void LoadData(MoneyChange entity)
        {
            ed_Name.SetValue(entity.Name);
            if (!entity.UserId.IsNull())
                ed_User.SetValue(entity.UserId);
            ed_MoneyType.SetValue(entity.MoneyType);
            ed_Code.SetValue(entity.Code);
            ed_MoneyBefore.SetValue(entity.MoneyBefore);
            ed_MoneyAfter.SetValue(entity.MoneyAfter);
            ed_Money.SetValue(entity.Money);
            ed_GroupId.SetValue(entity.GroupId);
        }

        protected override OperationResult SaveData(MoneyChange entity)
        {
            entity.Name = ed_Name.GetValue().ToString();
            if (ed_User.GetValue().ToLong() == 0)
                entity.UserId = null;
            else
                entity.UserId = ed_User.GetValue().ToLong();
            entity.MoneyType = ed_MoneyType.GetValue().ToInt();
            entity.Code = ed_Code.GetValue().ToString();
            entity.MoneyBefore = ed_MoneyBefore.GetValue().ToDouble();
            entity.MoneyAfter = ed_MoneyAfter.GetValue().ToDouble();
            entity.Money = ed_Money.GetValue().ToDouble();
            entity.GroupId = null;
            return base.SaveData(entity);
            
        }
    }
}
